package com.alibaba.system.controller;

import com.alibaba.basic.util.AjaxResult;
import com.alibaba.basic.util.PageList;
import com.alibaba.system.domain.SystemDictionaryType;
import com.alibaba.system.query.SystemDictionaryTypeQuery;
import com.alibaba.system.service.ISystemDictionaryTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 接口类：数字字典
 */
@RestController
@RequestMapping("/dictionaryType")
@Api(value = "店铺接口",description = "店铺的CRUD功能") // Swagger注解：对Controller做描述
public class SystemDictionaryTypeController {

    @Autowired
    private ISystemDictionaryTypeService dictionaryTypeService;

    /**
     * 接口：查询单个对象
     * http://localhost:8080/dictionarytype/1
     * @param id
     * @return
     */
    @GetMapping("/{id}") // 带参数的get请求
    @ApiOperation(value = "查询单个对象")
    public SystemDictionaryType findOne(@PathVariable("id") Long id) {
        return dictionaryTypeService.findOne(id);
    }

    /**
     * 接口：查询所有对象
     * http://localhost:8080/dictionarytype
     * @return
     */
    @GetMapping // 不带参数的get请求
    @ApiOperation(value = "查询所有对象")
    public List<SystemDictionaryType> findAll() {
        return dictionaryTypeService.findAll();
    }

    /**
     * 接口：分页查询 + 高级查询
     * http://localhost:8080/dictionarytype + {json对象}
     * @RequestBody: 从请求体【请求数据包中】中获取数据
     * @param dictionaryTypeQuery
     * @return
     *
     *
     */
    @PostMapping // 发送post请求，从请求体【请求数据包中】中获取分页查询 + 高级查询数据
    @ApiOperation(value = "分页查询")
    public PageList<SystemDictionaryType> queryPage(@RequestBody SystemDictionaryTypeQuery dictionaryTypeQuery) {
        return dictionaryTypeService.queryPage(dictionaryTypeQuery);
    }

    /**
     * 接口：删除
     * http://localhost:8080/dictionarytype/1
     * @return
     */
    @DeleteMapping("/{id}") // 发送删除请求
    @ApiOperation(value = "删除")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            dictionaryTypeService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败");
        }
    }

    /**
     * 接口：批量删除
     * http://localhost:8080/dictionarytype/1
     * @return
     */
    @PatchMapping() // 发送删除请求
    @ApiOperation(value = "批量删除")
    public AjaxResult batchDelete(@RequestBody Long[] ids) {
        try {
            dictionaryTypeService.batchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败");
        }
    }

    /**
     * 接口：添加或修改
     * http://localhost:8080/department + {json对象}
     * @RequestBody: 从请求体【请求数据包中】中获取数据
     * @param dictionarytype
     * @return
     */
    @PutMapping // 可以用于添加或修改资源
    @ApiOperation(value = "添加或修改")
    public AjaxResult addOrUpdate(@RequestBody SystemDictionaryType dictionarytype) {
        try {
            if (dictionarytype.getId() != null) { // 修改数据
                dictionaryTypeService.update(dictionarytype);
            } else { // 添加数据
                dictionaryTypeService.add(dictionarytype);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "操作失败");
        }
    }

}
